package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.exception.ManagerServiceException;
import com.itany.mapper.SupplierMapper;
import com.itany.pojo.Supplier;
import com.itany.service.SupplierService;
import com.itany.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    SupplierMapper supplierMapper;
    @Override
    public PageInfo<Supplier> findAllExample(Integer page, Integer rows, String name) {
        if (StringUtils.isEmpty(name)){
            name = null;
        }
        PageHelper.startPage(page, rows);
        List<Supplier> allWithName = supplierMapper.findAllWithName(name);
        PageInfo<Supplier> info = new PageInfo<>(allWithName);
        return info;
    }

    @Override
    public void update(Supplier supplier) throws ManagerServiceException {
        if (supplier.getId()==null){
            throw new ManagerServiceException("更新失败");
        }
        supplierMapper.updateByPrimaryKeySelective(supplier);
    }

    @Override
    public void add(Supplier supplier) {
        supplier.setCreatetime(new Date());
        supplierMapper.insertSelective(supplier);
    }
}
